package org.example.springredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/test")
public class MyController {
    //StringRedisTemplate是RedisTemplate的子类，用来传输文本数据的
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/string")
    public String testString() {
        //这里进行清空数据库
        //接口是一个函数式，使用lambd表达式
        redisTemplate.execute((RedisConnection connection) -> {
            //execute 要求回调方法中必须写return语句，返回个东西
            //这个回调返回的对象，就会作为execute本身的返回值
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");
        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value:"+value);
        return "OK";
    }
    @RequestMapping("/list")
    public String testList() {
        redisTemplate.execute((RedisConnection connection) -> {
            //execute 要求回调方法中必须写return语句，返回个东西
            //这个回调返回的对象，就会作为execute本身的返回值
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("list", "111");
        redisTemplate.opsForList().leftPush("list", "222");
        redisTemplate.opsForList().leftPush("list", "333");

        System.out.println(redisTemplate.opsForList().rightPop("list"));
        System.out.println(redisTemplate.opsForList().rightPop("list"));
        System.out.println("遍历列表："+redisTemplate.opsForList().range("list", 0, -1));
        return "OK";
    }
    @RequestMapping("/set")
    public String testSet() {
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForSet().add("set", "111","222","333","444");
        Set<String> members = redisTemplate.opsForSet().members("set");
        System.out.println(members);
        System.out.println(redisTemplate.opsForSet().size("set"));
        System.out.println(redisTemplate.opsForSet().isMember("set", "111"));
        System.out.println("删除元素的个数："+redisTemplate.opsForSet().remove("set","111","222"));
        return "OK";
    }
    @RequestMapping("/hash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
           return null;
        });
        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "222");
        redisTemplate.opsForHash().put("key", "f3", "333");
        //要想变成字符串的话，需要进行强转
        Set<Object> keys = redisTemplate.opsForHash().keys("key");
        System.out.println(keys);
        System.out.println(redisTemplate.opsForHash().hasKey("key", "f2"));
        System.out.println(redisTemplate.opsForHash().hasKey("key", "f100"));
        System.out.println("长度为："+redisTemplate.opsForHash().size("key"));
        System.out.println("删除元素 "+redisTemplate.opsForHash().delete("key","f1"));
        System.out.println("剩余长度为："+redisTemplate.opsForHash().size("key"));
        return "OK";
    }
    @RequestMapping("/zset")
    public String testZset() {
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForZSet().add("key","zhangsan",100);
        redisTemplate.opsForZSet().add("key","lisi",101);
        redisTemplate.opsForZSet().add("key","wangwu",102);
        redisTemplate.opsForZSet().add("key","zhaoliu",103);
        Set<ZSetOperations.TypedTuple<String>> tuples = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println(tuples);
        System.out.println("长度为："+redisTemplate.opsForZSet().size("key"));
        System.out.println("删除元素："+redisTemplate.opsForZSet().remove("key","zhaoliu"));
        System.out.println("分数为："+redisTemplate.opsForZSet().score("key","wangwu"));
        System.out.println("排名为："+redisTemplate.opsForZSet().rank("key","wangwu"));
        System.out.println("存在元素："+redisTemplate.opsForSet().isMember("key","zhaoliu"));
        return "OK";
    }
}
